package com.superb.system.service.impl;

import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.superb.common.core.enums.SuperbCode;
import com.superb.common.core.exception.SuperbException;
import com.superb.common.database.entity.BaseEntity;
import com.superb.common.database.mybatis.service.impl.SuperbServiceImpl;
import com.superb.common.utils.StringUtils;
import com.superb.system.api.entity.Organization;
import com.superb.system.mapper.SystemOrganizationMapper;
import com.superb.system.service.SystemOrganizationService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 部门机构;(system_organization)表服务实现类
 *
 * @Author: ajie
 * @CreateTime: 2024-6-14
 */
@Service
public class SystemOrganizationServiceImpl extends SuperbServiceImpl<SystemOrganizationMapper, Organization> implements SystemOrganizationService {
    @Override
    public void buildTree(Organization parentOrganization) {
        LambdaQueryWrapper<Organization> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Organization::getParentId, parentOrganization.getId());
        lqw.orderByAsc(Organization::getSort);
        List<Organization> organizations = super.list(lqw);
        if (organizations.size() > 0) {
            parentOrganization.setChildren(organizations);
            for (Organization organization : organizations) {
                buildTree(organization);
            }
        }
    }

    @Override
    public void getIdAndSub(List<String> organizetionIds, String organizetionId) {
        QueryWrapper<Organization> lqw = new QueryWrapper<>();
        lqw.select("id").lambda().eq(Organization::getParentId, organizetionId).orderByAsc(Organization::getSort);
        List<String> ids = super.listObjs(lqw, Object::toString);
        if (ids.size() > 0) {
            organizetionIds.addAll(ids);
            for (String id : ids) {
                getIdAndSub(organizetionIds, id);
            }
        }
    }

}